<template>
	<view :modelValue="sample" class="container" >
		<!-- 导航栏 -->
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			titleStyle="{height: '52px'}" :title="sample.name" @clickLeft="back">
			<block slot="right">
				<view class="city">
					<uni-icons type="more-filled" size="25" color="white" @click="doMore"></uni-icons>
				</view>
			</block>
		</uni-nav-bar>
		<uni-section title="当前样品状态" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms labelPosition="right" >
					<uni-forms-item label="样品状态" name="status" >
						<u--text style="padding: 7px 0;" :text="sample.status"></u--text>
					</uni-forms-item>
					<uni-forms-item label="当前处理人" name="currentHandlerName" >
						<u--text style="padding: 7px 0;" :text="sample.currentHandlerName" ></u--text>
					</uni-forms-item>
				</uni-forms>
			</view>
		</uni-section>
		
		<uni-section title="领样信息" type="line">
			<view class="example">
				<uni-forms-item label="领样部门" labelPosition="top">
					<uni-easyinput v-model="sample.consumeDepName" disabled />
				</uni-forms-item>
				<uni-forms-item label="领样人" labelPosition="top">
					<uni-easyinput v-model="sample.consumerName" disabled />
				</uni-forms-item>
			</view>
		</uni-section>
		
		<uni-section title="样品基本信息" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" labelPosition="top" :rules="rules">
					<uni-forms-item label="样品单位" name="department" required>
						<uni-easyinput v-model="sample.department" disabled=""/>
					</uni-forms-item>
					<uni-forms-item label="样品编码" name="code" required>
						<uni-easyinput v-model="sample.code" disabled />
					</uni-forms-item>
					<uni-forms-item label="入库时间">
						<uni-datetime-picker type="datetime" :clear-icon="false" v-model="sample.warehousingTime" disabled />
					</uni-forms-item>
					<uni-forms-item label="样品名称" name="name" required>
						<uni-easyinput v-model="sample.name" disabled=""/>
					</uni-forms-item>
					<uni-forms-item label="规格型号" name="specification" required>
						<uni-easyinput v-model="sample.specification" disabled=""/>
					</uni-forms-item>
					<uni-forms-item label="样品数量" name="number" required>
						<uni-easyinput v-model="sample.number" disabled=""/>
					</uni-forms-item>
					<uni-forms-item label="送样人" name="sender" required >
						<uni-easyinput v-model="sample.sender"disabled="" />
					</uni-forms-item>
					<uni-forms-item label="任务类型" name="taskType" required >
						<uni-easyinput v-model="sample.taskType"disabled="" />
					</uni-forms-item>
				</uni-forms>
			</view>
		</uni-section>
		<uni-section title="样品照片" type="line">
			<view class="example">
				<uni-file-picker limit="3" title="最多选择3张图片" :readonly="true" v-model="sample.imgLists" :auto-upload="true" file-mediatype="image">
				</uni-file-picker>
			</view>
		</uni-section>
		<uni-section title="样品存放地点" type="line">
			<view class="example">
				<uni-data-checkbox v-model="sample.storageLocation" :localdata="locations" mode="button" disabled="" />
				<uni-easyinput v-if="!isLocationInDictionary()" v-model="sample.storageLocation"
					placeholder="请输入其他地点信息" />
			</view>
		</uni-section>
		
		<u-action-sheet :actions="actions" @close="closeActionSheet" :show="showActionSheet" cancelText="取消"
			@select="selectClick"></u-action-sheet>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sample: {
					code: 'Y{' + this.$moment().format('YYYY-MM-DD-h-mm-ss') + '}',
					location: '',
					consumer: '',
					number: 1,
					consumeDep: '',
					name:'示例'
				},
				locations: [],
				orgNames: [],
				orgs: [],
				userNames: [],
				users: [],
				loading: false,
				showConfirmModal: false,
				rules: {
					// 对department字段进行必填验证
					department: {
						rules: [{
							required: true,
							errorMessage: '请输入单位名称',
						}]
					},
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入样品名称',
						}]
					},
					specification: {
						rules: [{
							required: true,
							errorMessage: '请输入规格型号',
						}]
					},
					sender: {
						rules: [{
							required: true,
							errorMessage: '请输入送样人名称',
						}]
					},
				},
				// 更多
				showActionSheet: false,
				actions: [],
				actionName2url: {
					'样品领取': '/pages/sample/my/recieve?id=',
					'查看详情': '/pages/sample/my/detail?id=',
					'继续流转': '/pages/sample/my/delegate?id=',
					'归还样品': '/pages/sample/my/sendback?id=',
					'委托归还': '/pages/sample/my/grantSendback?id=',
					'退样': '/pages/sample/my/fallback2Recieve?id=',
					'确认流转': '/pages/sample/my/delegateConfirm?id=',
					'拒绝流转': '/pages/sample/my/delegateReject?id=',
					'退审': '/pages/sample/my/fallback2Checking?id=',
				},
				tags: [{
						text: "检验中",
						actions: [{
								name: '继续流转'
							},
							{
								name: '归还样品'
							},
							{
								name: '委托归还'
							},
							{
								name: '退样'
							}
						]
					},
					{
							text: "归还委托中",
							actions: [{
									name: '继续流转'
								},
								{
									name: '归还样品'
								},
								{
									name: '委托归还'
								},
								{
									name: '退样'
								},
							]
						},
					{
						text: "归还待确认",
						actions: [{
								name: '退审'
							},
						]
					},
					{
						text: "待领取",
						actions: [{
								name: '样品领取'
							},
						]
					},
					{
						text: "流转待确认",
						checked: false,
						style: "float:left; margin: 10px 0px 10px 0px;",
						actions: [{
								name: '确认流转'
							},
							{
								name: '拒绝流转'
							}
						]
					}
				],
			}
		},
		
		onLoad(option) {
			// 加载样品存放地点(字典数据，Location)
			this.loadSampleInfo(option.id);
			this.loadDictData('Location');
			this.queryOrgList();
		},
		computed: {},
		methods: {
			confirmRecieve(){
				var this_ = this; //存储this
				this_.showConfirmModal = true;
			},
			confirm(){
				var this_ = this; //存储this
				this.$u.api.forwardSample(this_.sample).then(res=>{
					this_.$u.toast('领取成功');
					this_.back();
				});
			},
			//是否存放地址为自定义
			isLocationInDictionary(){
				var this_ = this; //存储this
				if(!this_.locations || !this_.sample.storageLocation) return false;
				for (var i = 0; i < this_.locations.length; i++) {
					if(this_.locations[i]==this_.sample.storageLocation) {
						return true;
					}
				}
				return false;
			},
			loadSampleInfo(sampleId){
				var this_ = this;
				this.$u.api.querySampleInfo(sampleId).then(res=>{
					this_.sample = res.data;
					let imgLists = [];
					//包装图片控件数据
					if(this_.sample.img1)
						imgLists.push({url:this_.sample.img1,name:'img1'})
					if(this_.sample.img2)
						imgLists.push({url:this_.sample.img2,name:'img2'})
					if(this_.sample.img3)
						imgLists.push({url:this_.sample.img3,name:'img3'})
					this_.sample.imgLists=imgLists;
					// 更多
					const tag = this_.tags.find((t) => t.text === this_.sample.status) || {}
					this_.actions = tag.actions || []
				});
			},
			userChanged(userName) {
				//如果填入的用户名称非法，则主动清空内容
				var userId = this.resolveUserIdWithSelectedUserName(userName);

				if (!userId) {
					this.sample.consumerName = '';
				} else {
					this.sample.consumerName = userName;
					this.sample.consumer = userId;
				}
			},
			orgChanged(orgName) {
				if (orgName === '') return;
				//如果填入的机构名称非法，则主动清空内容
				var orgId = this.resolveDepIdWithSelectedOrgName(orgName);
				if (!orgId) {
					this.sample.consumeDep = '';
					this.sample.consumeDepName = '';
					//机构切换,则领用人选择项清空,领用人选项刷新
				} else if (!(orgId === this.sample.consumeDep)) {
					this.selectedUserName = '';
					this.sample.consume = '';
					this.sample.consumeDep = orgId;
					this.queryUserList(orgId);
				}
			},
			resolveDepIdWithSelectedOrgName(orgName) {
				for (let index = 0; index < this.orgs.length; index++) {
					if (this.orgs[index].deptName === orgName) return this.orgs[index].deptId;
				}
				return null;
			},
			resolveUserIdWithSelectedUserName(userName) {
				for (let index = 0; index < this.users.length; index++) {
					if (this.users[index].userName === userName) return this.users[index].userId;
				}
				return null;
			},
			queryUserList(orgId) {
				var this_ = this; //存储this

				this.$u.api.queryOrgUserList(orgId).then((response) => {
					//这个控件只能支持字符串选取
					this_.users = response.rows;
					let userNames = [response.rows.length];
					for (let index = 0; index < response.rows.length; index++) {
						userNames[index] = response.rows[index].nickName;
					}
					this_.userNames = userNames;
				});
			},
			queryOrgList() {
				var this_ = this; //存储this
				this.$u.api.queryOrgList().then((response) => {
					//这个控件只能支持字符串选取
					this_.orgs = response.data;
					let orgNames = [response.data.length];
					for (let index = 0; index < response.data.length; index++) {
						orgNames[index] = response.data[index].deptName;
					}
					this_.orgNames = orgNames;
				});
			},
			loadDictData(dictType) {
				var this_ = this; //存储this
				this.$u.api.getDictData({
					"dictType": 'Location'
				}).then((response) => {
					let locations = [response.data.length];
					for (let index = 0; index < response.data.length; index++) {
						locations[index] = {
							'text': response.data[index].dictLabel,
							'value': response.data[index].dictValue
						};
					}
					locations[locations.length] = {
						'text': '其他地点',
						'value': 'custom'
					};
					this_.sample.location = locations[0].value;
					this_.locations = locations;
				});
			},
			back() {
				this.backPageRefresh();
			},
			modifySubmit() {
				var this_ = this;
				this.$refs.baseForm.validate().then(res => {
					//先上传图片,再提交
					this.$u.api.modifySample(this_.sample).then(response => {
						this_.$u.toast('保存成功');
						this_.back();
					});
				}).catch(err => {
					console.log('表单错误信息：', err);
					this.$u.toast('必填信息不能为空');
				})
			},
			doMore() {
				this.showActionSheet = true;
			},
			closeActionSheet() {
				this.showActionSheet = false;
			},
			selectClick(index) {
				//跳转到具体页面
				var route = this.actionName2url[index.name];
				if (route) {
					uni.navigateTo({
						url: route + this.sample.id
					});
				}
			},
		}
	}
</script>

<style>
	.uni-section {
		margin-top: 0px !important;
		background-color: #eee;
	}

	.custom-image-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.example {
		background-color: white;
		padding: 10px 20px 15px 20px;
	}

	.segmented-control {
		margin-bottom: 10px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
